
/* 属性分为: 1.数据属性 ---->  value,writable,enumerable,configurable
            2.存取器属性 ----> get,set,enumerable,configurable
    其中writable,enumerable,configurable的值都是布尔值(bool)
    而 get 和 set 是函数值 */

var People = {
    type: "human",
    name: "lin",
    $age: 28,
    get names(){
        return this.name;
    },
    set names(value){
        this.name = value;
    }
};
console.log(People.name);
console.log(People.names);
People.names = "hui";
console.log(People.names);
console.log(People.name);
console.log(People.$age);

/* 可以通过Object.getOwnPropertyDescriptor() 获取某个对象特定属性的属性描述 */

// eg：
console.log(Object.getOwnPropertyDescriptor(People,"name"));
console.log(Object.getOwnPropertyDescriptor(People,"names"));

/* 设置属性的特性 Object.defineProperty() Object.defineProperties() 设置某个对象特定属性的属性描述 */
// eg:
var O = {
    $age: 25,
    get age(){
        return this.$age;
    },
    set age(newValue){
        this.$age = newValue;
    }
};
Object.defineProperties(O,{
    name:{
        value: "lin",//对象属性的默认值，默认值为undefined
        writable: false,//对象属性是否可修改,flase为不可修改，默认值为true
        enumerable: true,//对象属性是否可通过for-in循环，flase为不可循环，默认值为true
        configurable: false,//能否使用delete、能否需改属性特性、或能否修改访问器属性、，false为不可重新定义，默认值为true
    },
    firstName:{
        value: "Li",
        writable: false,
        enumerable: true,
        configurable: true,
    },
    lastName:{
        value: "huilin",
        writable: true,
        enumerable: false,
        configurable: true,
    },
    $age: {
        // writable: true,
        writable: false, //false 的话$age属性不可修改。
        enumerable: false,
        configurable: false,
    },
    age: {
        enumerable: false,
        configurable: false,
    }
})
O.age = 33;
O.$age = 36;
console.log(O)
console.log(O.$age)

for(var index in O){
    console.log("遍历："+index+"--"+O[index])
}

/* 设置属性的特性 Object.defineProperty() */
var Obj = {
    $porp: {
        num: 22,
    }

};
Object.defineProperty(Obj,"$porp",{
    get(){
        return this.$porp;
    },
    enumerable: false,
    configurable: false,
});
Obj.$porp = {num:33}
console.log(Obj.$porp);

console.log(JSON.parse('{a:1}'));